/*
 * PanelTambahMember.java
 *
 * Created on 15 Desember 2011, 22:16
 */
package Boundary;

import Controller.DatabaseHandler;
import Controller.controllerMember;
import Entiti.Member;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author  Jeanot Nahasan
 */
public class PanelTambahMember extends javax.swing.JPanel {

    ImageIcon eror = new ImageIcon(getClass().getResource("/gambar/error.png"));

    /** Creates new form PanelTambahMember */
    public PanelTambahMember() {
        initComponents();
        this.setSize(960, 420);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txfNamaMember = new javax.swing.JTextField();
        txfNoHp = new javax.swing.JTextField();
        txfEmailMember = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txfAlamatMember = new javax.swing.JTextArea();
        lblStatusNoHp = new javax.swing.JLabel();
        lblStatusNama = new javax.swing.JLabel();
        lblStatusEmail = new javax.swing.JLabel();
        lblStatusTambahUser = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(960, 540));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Nama Lengkap");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 180, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("Alamat");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 300, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("No HP");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 260, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Email");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 220, -1, -1));

        txfNamaMember.setFont(new java.awt.Font("Tahoma", 0, 14));
        txfNamaMember.setToolTipText("Hanya huruf, karakter lain dan tanda baca akan mengakibatkan eror");
        txfNamaMember.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txfNamaMemberFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txfNamaMemberFocusLost(evt);
            }
        });
        txfNamaMember.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txfNamaMemberKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txfNamaMemberKeyTyped(evt);
            }
        });
        add(txfNamaMember, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 173, 220, 30));

        txfNoHp.setFont(new java.awt.Font("Tahoma", 0, 14));
        txfNoHp.setToolTipText("Hanya angka. Minimal 7 karakter");
        add(txfNoHp, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 253, 160, 30));

        txfEmailMember.setFont(new java.awt.Font("Tahoma", 0, 14));
        txfEmailMember.setToolTipText("contoh : admin@sebarinfo.com");
        add(txfEmailMember, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 213, 220, 30));

        btnCancel.setText("Cancel");
        btnCancel.setMaximumSize(new java.awt.Dimension(91, 39));
        btnCancel.setMinimumSize(new java.awt.Dimension(91, 39));
        btnCancel.setPreferredSize(new java.awt.Dimension(91, 39));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 400, 91, 39));

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setToolTipText("Klik untuk menyimpan data");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 400, 100, -1));

        txfAlamatMember.setColumns(20);
        txfAlamatMember.setFont(new java.awt.Font("Tahoma", 0, 14));
        txfAlamatMember.setRows(5);
        jScrollPane1.setViewportView(txfAlamatMember);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 300, -1, -1));

        lblStatusNoHp.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusNoHp.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusNoHp.setText("*");
        lblStatusNoHp.setToolTipText("wajib diisi. hanya boleh angka");
        add(lblStatusNoHp, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 260, -1, -1));

        lblStatusNama.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusNama.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusNama.setText("*");
        lblStatusNama.setToolTipText("wajib diisi. hanya huruf. tidak boleh mengandung karakter unik dan/atau angka");
        add(lblStatusNama, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 180, -1, -1));

        lblStatusEmail.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusEmail.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusEmail.setText("*");
        lblStatusEmail.setToolTipText("wajib diisi");
        add(lblStatusEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 220, -1, -1));

        lblStatusTambahUser.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusTambahUser.setText("Status :");
        add(lblStatusTambahUser, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 510, -1, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Tambah Member");
        jLabel6.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 130, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/bg.jpg"))); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
    }// </editor-fold>//GEN-END:initComponents
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.clearField();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void clearField() {
        this.txfAlamatMember.setText(null);
        this.txfNamaMember.setText(null);
        this.txfNoHp.setText(null);
        this.txfEmailMember.setText(null);
    }

   

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed

        Member member = new Member();
        
        controllerMember cont = new controllerMember();
        int id = cont.autoGenerateID();
        String nama = this.txfNamaMember.getText();
        String alamat = this.txfAlamatMember.getText();
        String email = this.txfEmailMember.getText();
        String noHp = this.txfNoHp.getText();

        if (!member.isNamaValid(nama)) {
            lblStatusNama.setIcon(eror);
            lblStatusNama.setText("Error!");
            lblStatusTambahUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            this.lblStatusTambahUser.setText("Penambahan Member Baru Gagal!");
        } else {
            lblStatusNama.setIcon(null);
            lblStatusNama.setText("");
        }
        if (!member.isNoHpMemberValid(noHp)) {
            lblStatusNoHp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusNoHp.setText("Error!");
            lblStatusTambahUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            this.lblStatusTambahUser.setText("Penambahan Member Baru Gagal!");
        } else {
            lblStatusNoHp.setIcon(null);
            lblStatusNoHp.setText("");
        }
        if (!member.isEmailMemberValid(email)) {
            lblStatusEmail.setIcon(eror);
            lblStatusEmail.setText("Error!");
            lblStatusTambahUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            this.lblStatusTambahUser.setText("Penambahan Member Baru Gagal!");
        } else {
            lblStatusEmail.setIcon(null);
            lblStatusEmail.setText("");
        }
        if (member.isNamaValid(nama) == true && member.isEmailMemberValid(email) == true && member.isNoHpMemberValid(noHp) == true) {
            if (cont.tambahMember(id, nama, noHp, alamat, email) == true) {
                clearField();
                lblStatusTambahUser.setIcon(null);
                this.lblStatusTambahUser.setText("Penambahan Member Baru Berhasil!");
            } else {
                lblStatusTambahUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
                this.lblStatusTambahUser.setText("Penambahan Member Baru Gagal! Terjadi kesalahan saat menyimpan data ke database");
            }
        }

    }//GEN-LAST:event_btnSaveActionPerformed

    private void txfNamaMemberKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txfNamaMemberKeyReleased
    // TODO add your handling code here:
    }//GEN-LAST:event_txfNamaMemberKeyReleased

    private void txfNamaMemberKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txfNamaMemberKeyTyped
     
    }//GEN-LAST:event_txfNamaMemberKeyTyped

    private void txfNamaMemberFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfNamaMemberFocusGained
        txfNamaMember.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 255), 1, true)); // TODO add your handling code here:
    }//GEN-LAST:event_txfNamaMemberFocusGained

    private void txfNamaMemberFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfNamaMemberFocusLost
        txfNamaMember.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 0, true)); 
    }//GEN-LAST:event_txfNamaMemberFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblStatusEmail;
    private javax.swing.JLabel lblStatusNama;
    private javax.swing.JLabel lblStatusNoHp;
    private javax.swing.JLabel lblStatusTambahUser;
    private javax.swing.JTextArea txfAlamatMember;
    private javax.swing.JTextField txfEmailMember;
    private javax.swing.JTextField txfNamaMember;
    private javax.swing.JTextField txfNoHp;
    // End of variables declaration//GEN-END:variables
}
